﻿using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace Spider.HaiDaoWan
{
    class Program
    {
        //34100
        static async Task Main(string[] args)
        {
            if (!Directory.Exists("success"))
            {
                Directory.CreateDirectory("success");
            }
            int start = 0;
            if (args.Length > 0)
            {
                start = int.Parse(args[0]);
            }
            
            var httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri("https://www.yinfans.net");

            for (var i = start; i <= 999999; i++)
            {
                try
                {
                    Console.WriteLine($"开始处理序号[{i}]");
                    var response = await httpClient.GetAsync($"/m_{i}.html");
                    Console.WriteLine($"{i}\t : {response.StatusCode}");
                    if (response.IsSuccessStatusCode)
                    {
                        var content = await response.Content.ReadAsStringAsync();
                        var prefix = i.ToString("000000");
                        await Analyzing(prefix, content);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"== {i} 发生错误 ==");
                    Console.WriteLine(ex);
                }
                await Task.Delay(200);
            }
        }

        static async Task Analyzing(string prefix, string html)
        {
            var document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);

            var title = document.DocumentNode.SelectSingleNode("//title")?.InnerText;
            if (string.IsNullOrWhiteSpace(title))
            {
                title = prefix;
            }
            Console.WriteLine($"Title: {title}");
            foreach (var invalidChar in Path.GetInvalidFileNameChars())
            {
                title = title.Replace(invalidChar, '_');
            }
            title = title.Replace("海盗湾", "");
            title = title.TrimEnd('_');
            title = title.TrimEnd('-');
            title = title.Replace("__", "_");
            title = title.Replace("__", "_");
            title = title.Replace("__", "_");
            title = title.Substring(0, title.Length > 240 ? 240 : title.Length);
            document.Save($"success/{prefix}_{title}.html");
        }
    }
}
